#!/bin/sh /etc/rc.common
# Copyright (C) 2017 OpenWrt.org

START=92
USE_PROCD=1
PROG=/usr/bin/baresip
ACCOUNTS=

add_account()
{
	local user password server transport append ignore mediaenc
	local acc

	uci_validate_section baresip account "${1}" \
		'ignore:bool:0' \
		'user:string' \
		'password:string' \
		'server:string' \
		'transport:string:udp' \
		'mediaenc:string' \
		'append:string'

	[ "$ignore" = "0" ] || return

	acc="<sip:${user}@${server};transport=${transport}>"
	[ -n "$mediaenc" ] && acc="${acc};mediaenc=${mediaenc}"
	[ -n "$password" ] && acc="${acc};auth_pass=${password}"
	[ -n "$append" ] && acc="${acc};${append}"

	ACCOUNTS="${ACCOUNTS}${acc}\n"
}

start_service()
{
	local enable options

	uci_validate_section baresip baresip main \
		'enable:bool:0' \
		'options:string'

	[ "$enable" = "0" ] && return

	config_load baresip
	config_foreach add_account account
	printf "$ACCOUNTS" > /etc/baresip/accounts

	procd_open_instance
	procd_set_param command $PROG -f /etc/baresip $options
	procd_set_param user baresip
	procd_set_param respawn
	procd_close_instance
}

service_triggers()
{
	procd_add_reload_trigger baresip
}

reload_service()
{
	/etc/init.d/baresip restart
}
